/* 
 *  This file is part of hmvcBlog-gwt-rpc.
 *
 *  hmvcBlog-gwt-rpc is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-gwt-rpc is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-gwt-rpc.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user;

import com.google.gwt.core.client.GWT;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLoginDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLogoutDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserSessionId;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.EMailInvalidException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginExistException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.NotLoggedInException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.PasswordEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.PasswordWrongException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.events.NotLoggedInEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.LoginEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.LoginNotFoundEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.PasswordEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.AddUserSuccessfulEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.EMailInvalidEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.LoginExistEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.ReceiveUserEditDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.out.UserUpdateSucessfullEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.out.LoginSuccessfulEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.out.PasswordWrongEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.logout.out.LogoutSuccessfulEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.remove.out.UserRemoveSuccessfulEvent;

import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.IEventListener;

import com.googlecode.hmvc4gwt.framework.client.rpc.ASessionAsyncCallback;

/**
 * 
 *
 * @author jens.meiss
 */
public class UserAsyncCallback<T, E extends IEvent> extends ASessionAsyncCallback<T, E> {

	private static IUserRemoteServiceAsync remoteService;
	
	public static void addUser(UserNewDTO userNewDTO, IEventListener eventListener) {
		
		UserAsyncCallback<Long, AddUserSuccessfulEvent> asyncCallback = new UserAsyncCallback<Long, AddUserSuccessfulEvent>();
		asyncCallback.setEventListener(eventListener);
		asyncCallback.setEventOnSuccess(new AddUserSuccessfulEvent());
		
		getRemoteService().addUser(userNewDTO, asyncCallback);
		
	}
	
	public static void edit(IEventListener eventListener) {
		
		UserAsyncCallback<UserEditDTO, ReceiveUserEditDTOEvent> callback = new UserAsyncCallback<UserEditDTO, ReceiveUserEditDTOEvent>();
		callback.setEventOnSuccess(new ReceiveUserEditDTOEvent());
		callback.setEventListener(eventListener);
		
		UserSessionId sessionId = new UserSessionId();
		sessionId.setSessionId(getSessionId());
		
		getRemoteService().edit(sessionId, callback);
		
	}
	
	private static IUserRemoteServiceAsync getRemoteService() {
		
		if (remoteService == null) {
			remoteService = GWT.create(IUserRemoteService.class);
		}
		
		return remoteService;
		
	}
	
	public static void login(UserLoginDTO userLoginDTO, IEventListener eventListener) {
		
		UserAsyncCallback<Long, LoginSuccessfulEvent> asyncCallback = new UserAsyncCallback<Long, LoginSuccessfulEvent>();
		asyncCallback.setEventOnSuccess(new LoginSuccessfulEvent());
		asyncCallback.setEventListener(eventListener);
	
		getRemoteService().login(userLoginDTO, asyncCallback);
	}

	public static void logout(IEventListener eventListener) {
		
		/*
		 * Session zurück setzen
		 */
		UserLogoutDTO logoutDTO = new UserLogoutDTO(sessionId);
		setSessionId(0L);
		
		UserAsyncCallback<Void, IEvent> asyncCallback = new UserAsyncCallback<Void, IEvent>();
		asyncCallback.setEventOnSuccess(new LogoutSuccessfulEvent());
		asyncCallback.setEventListener(eventListener);
		
		getRemoteService().logout(logoutDTO, asyncCallback);		
	}

	public static void removeUser(IEventListener eventListener) {
		
		UserRemoveDTO userRemoveDTO = new UserRemoveDTO(sessionId);
		UserAsyncCallback<Void, IEvent> asyncCallback = new UserAsyncCallback<Void, IEvent>();
		asyncCallback.setEventOnSuccess(new UserRemoveSuccessfulEvent());
		asyncCallback.setEventListener(eventListener);
		
		getRemoteService().removeUser(userRemoveDTO, asyncCallback);
		
	}
	
	public static void update(UserEditDTO userEditDTO, IEventListener eventListener) {
		
		UserAsyncCallback<Void, UserUpdateSucessfullEvent> callback = new UserAsyncCallback<Void, UserUpdateSucessfullEvent>();
		callback.setEventOnSuccess(new UserUpdateSucessfullEvent());
		callback.setEventListener(eventListener);
		
		userEditDTO.setSessionId(getSessionId());
		
		getRemoteService().update(userEditDTO, callback);
	}
	
	public void onFailure(Throwable caught) {
		
		if (caught.getClass() == LoginExistException.class) {
			
			getEventListener().handleEvent(new LoginExistEvent());
			
		} else if (caught.getClass() == PasswordEmptyException.class) {
			
			getEventListener().handleEvent(new PasswordEmptyEvent());
			
		} else if (caught.getClass() == LoginEmptyException.class) {
			
			getEventListener().handleEvent(new LoginEmptyEvent());
			
		} else if (caught.getClass() == EMailInvalidException.class) {
			
			getEventListener().handleEvent(new EMailInvalidEvent());
			
		} else if (caught.getClass() == LoginNotFoundException.class) {
			
			getEventListener().handleEvent(new LoginNotFoundEvent());
			
		} else if (caught.getClass() == PasswordWrongException.class) {
			
			getEventListener().handleEvent(new PasswordWrongEvent());
			
		} else if (caught.getClass() == NotLoggedInException.class) {
			
			getEventListener().handleEvent(new NotLoggedInEvent());
			
		}		
		
	}
}

